//引入样式
require('../less/mine.less')

//引入字体
require('../fonts/iconfont.css')


//引入样式
require('weui')
let weui = require('weui.js')




//文档加载事件

document.ready(function () {

    //判断用户是否登录
    until.isLogin()

    //获取
    const medal = document.querySelectorAll('#medal')
    const out = document.querySelector('#out')
    const material = document.querySelector('#material')
    let nickname = document.querySelector('#nickname')
    let headimg = document.querySelector('#headimg')
    let sign = document.querySelector('#sign')
    let sportsdata = document.querySelector('#sportsdata')



    let numSports = document.querySelector('#numSports')
    let numCalorie = document.querySelector('#numCalorie')


    //点击事件
    medal.forEach(function (value, index) {
        value.addEventListener('click', function () {
            location.href = "./medal.html"
        })
    })
    //点击进入运动数据
    sportsdata.addEventListener('click',function(){
        location.href='./sportsdata.html'
    })

    //退出登入
    out.addEventListener('click', function () {
        

        //清提示
        weui.confirm('是否确定退出',function(){
            //清空本地数据
            localStorage.clear()
            //跳转
            location.href = "./enter.html"
        })
 
        
    })

    //头部点击进入个人资料
    material.addEventListener('click', function () {
        location.href = "./material.html"
    })



    //获取个人信息
    function getUserInfo() {
        //获取本地存储中的用户id
        const id = localStorage.getItem('userId')

        //发请求
        $http.get(`/users/accountinfo?userId=${id}`, function (res) {
            

            if (res.status === 0) {
                const userInfo = res.data


                //保存个人信息到本地存储中
                localStorage.setItem('userInfo', JSON.stringify(userInfo))


                //昵名
                nickname.textContent = userInfo.nickname
                


                //头像
                if (userInfo.imgurl) headimg.src = 'http://139.9.177.51:8099' + userInfo.imgurl

                // //签名
                if (userInfo.sign) sign.textContent = userInfo.sign
            }
        })

        //运动数据信息请求
        $http.get(`/users/mysportsBadge?userId=${id}`,function(res){
            if(res.status===0){
                //渲染总公里数
                numSports.textContent=res.data.sports.miles

                //渲染总卡路里数
                numCalorie.textContent=(res.data.sports.calorie).toFixed(2)
                
            }
            
        })
    }
    getUserInfo()


    //动态导航
    until.showTabs('mine')
})